package com.neusoft.neuiotms.dcom.repository.impl;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.model.Project;
import com.neusoft.neuiotms.dcom.model.QProject;
import com.neusoft.neuiotms.dcom.repository.CustomizedProjectRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedProjectRepositoryImpl extends AbstractRepository implements CustomizedProjectRepository {

    @Override
    public List<Project> index(final Searchable searchable) {
        final QProject qProject = com.neusoft.neuiotms.dcom.model.QProject.project;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<Project> query = this.query().selectFrom(qProject);
        query.where(where);
        return query.fetch();
    }

    @Override
    public Page<Project> page(final Searchable searchable, final Pageable pageable) {
        final QProject qProject = com.neusoft.neuiotms.dcom.model.QProject.project;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<Project> query = this.query().selectFrom(qProject);
        query.where(where);
        return super.search(query, pageable);
    }

    @Override
    protected Class<?> getModelClass() {
        return Project.class;
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {
        final BooleanBuilder where = new BooleanBuilder();
        final QProject qProject = com.neusoft.neuiotms.dcom.model.QProject.project;
        // 项目名称
        where.and(super.containsStr(qProject.name, searchable, "name"));
        return where;
    }
}
